package cn.xiaohuodui.service;

import cn.xiaohuodui.constant.ErrorCodeDefine;
import cn.xiaohuodui.dao.*;
import cn.xiaohuodui.dic.CategoryLevel;
import cn.xiaohuodui.exceptions.BussException;
import cn.xiaohuodui.model.*;
import cn.xiaohuodui.res.JsonRes;
import cn.xiaohuodui.vo.index.CateRecGoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Hash;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by TR on 2018/3/30.
 */
@Service
public class HomeService {

    @Autowired
    BannerMapper bannerMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    GoodMapper goodMapper;

    @Autowired
    TopicMapper topicMapper;


    /**
     * 获取首页数据
     * @return
     */
    public JsonRes<Map<String,Object>>  getHomeData() throws BussException {
        Map<String, Object> map = new HashMap<>();

        List<Banner> byPosition = bannerMapper.getByPosition(0);
        map.put("banners",byPosition);

        List<Category> categoriesByLevel = categoryMapper.getCategoriesByLevel(CategoryLevel.L1.getDesc());
        map.put("categories",categoriesByLevel);

        List<Brand> brandsIndex = brandMapper.getBrandsIndex();
        map.put("brands",brandsIndex);

        //首页推荐的新品商品
        List<Good> indexNewGoods = goodMapper.getIndexNewGoods(4);
        map.put("newGoods",indexNewGoods);

        //首页人气推荐商品
        List<Good> indexHotGoods = goodMapper.getIndexHotGoods(3);
        map.put("hotGoods",indexHotGoods);

        List<Topic> indexTopics = topicMapper.getIndexTopics(3);
        map.put("topics",indexTopics);

        List<CateRecGoodsVo> floorGoods = new ArrayList<>();

        for(Category category:categoriesByLevel){
            List<Good> categoryGoods = goodMapper.getCategoryGoods(category.getId());
            if (categoryGoods.size()>0) {
                CateRecGoodsVo cateRecGoodsVo = AssembleCateRecGoodsVo(categoryGoods);
                floorGoods.add(cateRecGoodsVo);
            }
        }

        map.put("floorGoods",floorGoods);

        return JsonRes.createBySuccessData(map);

    }

    //装配方法
    public  CateRecGoodsVo AssembleCateRecGoodsVo(List<Good> goods ) throws BussException {
        Integer category1Id = goods.get(0).getCategory1Id();
        Category category = categoryMapper.selectByPrimaryKey(category1Id);
        if(category==null){
            throw new BussException(ErrorCodeDefine.CATEGORY_NOT_EXIST.getCode(),ErrorCodeDefine.CATEGORY_NOT_EXIST.getDesc());
        }
        CateRecGoodsVo cateRecGoodsVo = new CateRecGoodsVo();
        cateRecGoodsVo.setName(category.getName());
        cateRecGoodsVo.setId(category.getId());
        cateRecGoodsVo.setGoodList(goods);
        return cateRecGoodsVo;
    }

}
